#include "LanguageManager.h"
#include "NGLog.h"
#include "GameDefine.h"

#include <iostream>
using namespace std;

void string_replace(string&s1,const string&s2,const string&s3)
{
	string::size_type pos=0;
	string::size_type a=s2.size();
	string::size_type b=s3.size();
	while((pos=s1.find(s2,pos))!=string::npos)
	{
		s1.replace(pos,a,s3);
		pos+=b;
		
		break;
	}
}

void string_replace_2(string&s1,const string&s2,const string&s3)
{
	string::size_type pos=0;
	string::size_type a=s2.size();
	string::size_type b=s3.size();
	while((pos=s1.find(s2,pos))!=string::npos)
	{
		s1.erase(pos,a);
		s1.insert(pos,s3);
		pos+=b;
		
		break;
	}
}

LanguageManager::LanguageManager()
{
	m_configPath = "";
	m_mapLanguageDic.clear();
}

LanguageManager::~LanguageManager()
{
	
}
	
bool LanguageManager::LoadXml()
{
	if(m_configPath.empty())
	{
		Log.Debug("LoadXml failed","文件路径m_configPath为空！");
		return false;
	}
	return LoadXml(m_configPath);
}

bool LanguageManager::LoadXml(string str)
{
	char strPath[512] = "";
	sprintf(strPath,"%s/%s",_GAME_XML_LANGUAGE_PATH_,str.c_str());
	TiXmlDocument doc(strPath);
	
	if(!doc.LoadFile())
	{
		Log.Debug("LoadXml failed","LoadFile 失败！");
		return false;
	}
	
	TiXmlElement* pRoot = doc.RootElement();
	TiXmlElement* pChild = pRoot->FirstChildElement();
	
	while(pChild != NULL)
	{
		string path = pChild->Attribute("srcPath");
		
		Log.Debug("加载这个语言的XML文件:",path.c_str());
		
		if(!LoadXmlByLanguage(path))
		{
			Log.Debug("这个Xml语言的信息加载失败:",path.c_str());
		}
		
		pChild = pChild->NextSiblingElement();
	}
	
	return true;
}

bool LanguageManager::LoadXmlByLanguage(string str)
{
	if(str.empty())
	{
		Log.Debug("通过语言加载XML失败:","路径为空！");
		return false;
	}
	
	
	char strPath[512] = "";
	sprintf(strPath,"%s/%s",_GAME_XML_LANGUAGE_PATH_,str.c_str());
	TiXmlDocument doc(strPath);
	
	if(!doc.LoadFile())
	{
		Log.Debug("在加载语言loadFile failed!:",str.c_str());
		return false;
	}
	
	TiXmlElement* pRoot = doc.RootElement();
	
	Language language = (Language)atoi(pRoot->Attribute("language"));
	
	TiXmlElement* pChild = pRoot->FirstChildElement();
	
	MapInfo mapInfo;
	
	while(pChild!=NULL)
	{
		uint16 id = atoi(pChild->Attribute("id"));
		string content = pChild->Attribute("content");

		Log.Debug("LoadXmlByLanguage","id[%d],content[%s]",id,content.c_str());
		
		mapInfo.insert(make_pair(id,content));
	
		pChild = pChild->NextSiblingElement();
	}
	
	m_mapLanguageDic.insert(make_pair(language,mapInfo));
	
	return true;
}

//重新加载
bool LanguageManager::ReloadXml()
{
	m_mapLanguageDic.clear();
	
	return LoadXml();
}

bool LanguageManager::ReloadXml(string str)
{
	m_mapLanguageDic.clear();
	
	return LoadXml(str);
}

//获取内容
bool LanguageManager::FinalInfo(Language language,int infoID,string & str)
{
	map<Language,MapInfo>::iterator fit1 = m_mapLanguageDic.find(language);
	if(fit1 == m_mapLanguageDic.end())
	{
		return false;
	}
	
	map<uint16,string> tempMapInfo = fit1->second;
	
	map<uint16,string>::iterator fit2 =  tempMapInfo.find(infoID);
	if(fit2 == tempMapInfo.end())
	{
		return false;
	}
	
	str = fit2->second;
	
	cout<<"（没有参数）当前信息："<<str<<endl;
	
	return true;
}

bool LanguageManager::FinalInfo(Language language,int infoID,map<string,string> replaceParam,string & str)
{
	if(!FinalInfo(language,infoID,str))
	{
		return false;
	}
	
	map<string,string>::iterator iter,eiter = replaceParam.end();
	for(iter = replaceParam.begin();iter != eiter; ++iter)
	{
		string key = "{" + iter->first + "}";
		string_replace(str,key,iter->second);
	}
	
	cout<<"（有参数）当前信息："<<str<<endl;
	
	return true;
}


